//
//  MGTabBarController.m
//  MortgageHelper
//
//  Created by apple on 16/6/28.
//  Copyright © 2016年 zhangxiang. All rights reserved.
//

#import "MGTabBarController.h"

#import "MGRequestService+My.h"
#import "MGConstFile.h"

#import "SLHomeViewController.h"
#import "SLInformationViewController.h"
#import "SLAboutMeViewController.h"


#define TabBarController [MGTabBarController sharedManager]

@interface MGTabBarController ()<UITabBarControllerDelegate>

@end

@implementation MGTabBarController


+(MGTabBarController *)sharedManager
{
    static MGTabBarController *_sharedManger;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _sharedManger = [[MGTabBarController alloc] init];
        _sharedManger.delegate=_sharedManger;
    });
    return _sharedManger;
}


- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;

    self.tabBar.barTintColor = [UIColor whiteColor];
    
    //测试 0:三个控制器  1:两个控制器
    //添加子控制器
    
    
    SLHomeViewController *MianVc = [[SLHomeViewController alloc]init];
    [self addChildViewController:MianVc withTitle:@"首页" normalImage:@"tab_home_nor" selectedImage:@"tab_home_hover"];
    
    SLInformationViewController *OrderVc = [[SLInformationViewController alloc]init];
    [self addChildViewController:OrderVc withTitle:@"贷款产品" normalImage:@"tab_list_nor" selectedImage:@"tab_list_hover"];
    
    SLAboutMeViewController *MyVc = [[SLAboutMeViewController alloc]init];
    [self addChildViewController:MyVc withTitle:@"我" normalImage:@"tab_my_nor" selectedImage:@"tab_my_hover"];
    
    
    //    设置tababrItem
    UITabBarItem *tabBarItem = [UITabBarItem appearance];
    NSDictionary *normalAttrDic = @{NSFontAttributeName : [UIFont systemFontOfSize:11],NSForegroundColorAttributeName:[UIColor defaultTextColor]};
    [tabBarItem setTitleTextAttributes:normalAttrDic forState:UIControlStateNormal];
    
    NSDictionary *selectedAttrDic = @{NSFontAttributeName : [UIFont systemFontOfSize:11],NSForegroundColorAttributeName:[UIColor themeColor]};
    [tabBarItem setTitleTextAttributes:selectedAttrDic forState:UIControlStateSelected];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(selectFristTapVc) name:MG_USER_LOGIN object:nil];
    
}

-(void)selectFristTapVc{
    self.selectedIndex=0;
}

#pragma mark - 添加子控制器
- (void)addChildViewController:(UIViewController *)childController withTitle:(NSString *)title normalImage:(NSString *)normalImage selectedImage:(NSString *)selectedImage
{
    childController.title = title;
    childController.tabBarItem.image = [UIImage imageNamed:normalImage];
    childController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    ZXBaseNavigationViewController *navVc = [[ZXBaseNavigationViewController alloc]initWithRootViewController:childController];
    [self addChildViewController:navVc];
}

#pragma mark - 是否 选中 子控制器
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    NSLog(@"ctr = %@",[viewController class]);
    
//    if(viewController == [tabBarController.viewControllers objectAtIndex:1]||viewController == [tabBarController.viewControllers objectAtIndex:2]||viewController == [tabBarController.viewControllers objectAtIndex:3])
//    {
//        
//        [UserInfoManage DetermineWhetherTheLoginWithVc:self.selectedViewController callback:^(BOOL success) {
//            if (success) {
//                TabBarController.selectedViewController=viewController;
//            }
//        }];
//        
//        return NO;
//        
//    }
    
    return YES;
    
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end

